package org.example.Thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Author : zhangbo
 * @data : 2024/6/19
 * @description :
 */
public class SingleThreadExecutor {
    /**
     * 只创建唯一的线程来执行任务。保证任务按照指定顺序（FIFO,LIFO，优先级）执行。
     * 如果这个线程异常结束，会有另一个取代它
     */
    public static void main(String[] args) {
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            final int index = i;
            singleThreadExecutor.execute(new Runnable() {
                public void run() {
                    try {
                        System.out.println(index);
                        Thread.sleep(8000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
